﻿using System;
using System.Windows;
using System.Xml.Linq;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void ClearDocColorSolution()
        {
            if (System.IO.File.Exists(Globals.pathOfPreferences))
            {
                MessageBoxResult r = MessageBox.Show("此操作将清除“新建文档默认配色方案”，清除后不可恢复。要继续吗？",
                    Globals.appName, MessageBoxButton.YesNo, MessageBoxImage.Warning);
                if (r != MessageBoxResult.Yes) return;

                try
                {
                    System.IO.File.Delete(Globals.pathOfPreferences);
                    MessageBox.Show("已清除此前保存的配色方案。重新启动程序后会恢复程序初始配色方案。", Globals.appName,
                        MessageBoxButton.OK, MessageBoxImage.Information);
                }
                catch (Exception ex)
                {
                    Dialogs.LunarMsgBox.MsgBoxShow(
                        Globals.appName,
                        "未清除“新建文档默认配色方案",
                        "尝试清除相关记录时出错，似乎执行了不必要的清除操作。" +
                        "异常信息如下：\r\n" + ex.Message,
                        ex.StackTrace, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            else
            {
                MessageBox.Show("没有找到“新建文档默认配色方案”相关记录。无须清除。",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
        }

        public static void SaveDocColorSolution()
        {
            if (Globals.selectedEditor == null)
            {
                MessageBox.Show("当前未打开任何文档！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (MessageBoxResult.Yes != MessageBox.Show("此操作会把当前文档的配色方案（含“默认背景色、" +
                "默认前景色、子面板选取框色、普通公式选取框色及１－４级文本标题的默认色彩”）作为程序下次启动后新建文档的默认配色方案。要继续吗？",
                Globals.appName, MessageBoxButton.YesNo, MessageBoxImage.Warning))
                return;

            if (System.IO.File.Exists(Globals.pathOfPreferences))
            {
                XDocument xDoc = XDocument.Load(/*"file:///" +加这个会出错！！*/ Globals.pathOfPreferences);

                XElement newDocDefSettingsElement = xDoc.Root.Element("NewDocDefSettings");
                newDocDefSettingsElement.SetElementValue("DefBackground",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefaultBackground));
                newDocDefSettingsElement.SetElementValue("DefForeground",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefaultForeground));
                newDocDefSettingsElement.SetElementValue("DefSelBox1Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.ExSelectionAdornerBrush));
                newDocDefSettingsElement.SetElementValue("DefSelBox2Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.SubPanelSelectionAdornerBrush));

                newDocDefSettingsElement.SetElementValue("DefHeaderText1Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText1Brush));
                newDocDefSettingsElement.SetElementValue("DefHeaderText2Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText2Brush));
                newDocDefSettingsElement.SetElementValue("DefHeaderText3Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText3Brush));
                newDocDefSettingsElement.SetElementValue("DefHeaderText4Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText4Brush));

                try
                {
                    xDoc.Save(Globals.pathOfPreferences);
                    MessageBox.Show("已保存此配色方案。重新启动程序后才会生效。", Globals.appName,
                        MessageBoxButton.OK, MessageBoxImage.Information);
                }
                catch (Exception ex)
                {
                    Dialogs.LunarMsgBox.MsgBoxShow(
                        Globals.appName,
                        "配色方案保存时出错。",
                        "异常信息如下：\r\n" + ex.Message,
                        ex.StackTrace, MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            else
            {
                XDocument xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));

                XElement rootElement = new XElement("Root");
                xDoc.Add(rootElement);

                XElement newDocDefSettingsElement = new XElement("NewDocDefSettings");
                rootElement.Add(newDocDefSettingsElement);

                XElement defBackgroundElement = new XElement("DefBackground",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefaultBackground));

                XElement defForegroundElement = new XElement("DefForeground",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefaultForeground));

                XElement defSelBox1ColorElement = new XElement("DefSelBox1Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.ExSelectionAdornerBrush));

                XElement defSelBox2ColorElement = new XElement("DefSelBox2Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.SubPanelSelectionAdornerBrush));

                newDocDefSettingsElement.Add(defBackgroundElement);
                newDocDefSettingsElement.Add(defForegroundElement);
                newDocDefSettingsElement.Add(defSelBox1ColorElement);
                newDocDefSettingsElement.Add(defSelBox2ColorElement);

                XElement defHeaderText1ColorElement = new XElement("DefHeaderText1Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText1Brush));

                XElement defHeaderText2ColorElement = new XElement("DefHeaderText2Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText2Brush));

                XElement defHeaderText3ColorElement = new XElement("DefHeaderText3Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText3Brush));

                XElement defHeaderText4ColorElement = new XElement("DefHeaderText4Color",
                    ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText4Brush));

                newDocDefSettingsElement.Add(defHeaderText1ColorElement);
                newDocDefSettingsElement.Add(defHeaderText2ColorElement);
                newDocDefSettingsElement.Add(defHeaderText3ColorElement);
                newDocDefSettingsElement.Add(defHeaderText4ColorElement);

                try
                {
                    xDoc.Save(Globals.pathOfPreferences);
                    MessageBox.Show("已保存此配色方案。重新启动程序后才会生效。", Globals.appName,
                        MessageBoxButton.OK, MessageBoxImage.Information);
                }
                catch (Exception ex)
                {
                    Dialogs.LunarMsgBox.MsgBoxShow(
                        Globals.appName,
                        "配色方案保存时出错。",
                        "异常信息如下：\r\n" + ex.Message,
                        ex.StackTrace, MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }
    }
}
